package com.sskj.message.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sskj.core.exception.MException;
import com.sskj.message.domain.MessageQueryParam;
import com.sskj.message.vo.MessageResultVO;
import com.my.utils.HttpUtils;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "/wechat/message", method = { RequestMethod.POST, RequestMethod.GET })
@Transactional
public class MessageServiceImpl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/vcode/send", method = { RequestMethod.GET })
	@ApiOperation(value = "发送验证码", notes = "发送验证码", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "phone", value = "电话", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "vcode", value = "验证码", paramType = "form", dataType = "string") })
	public MessageResultVO sendVCodeMessage(@ApiIgnore MessageQueryParam messageQueryParam) throws MException {

		Map<String, String> querys = new HashMap<String, String>();
		querys.put("ParamString", "{'CAPTCHA':'" + messageQueryParam.getVcode() + "'}");
		querys.put("RecNum", messageQueryParam.getPhone());
		querys.put("SignName", "升思科技");
		querys.put("TemplateCode", "SMS_70580574");

		logger.debug(JSON.toJSONString(querys));
		return sendMessage(querys);
//		MessageResultVO result = new MessageResultVO();
//		result.setSuccess(true);
//		result.setMessage("发送成功");
//		return result;

	}

	public MessageResultVO sendMessage(Map<String, String> querys) {

		String host = "http://sms.market.alicloudapi.com";
		String path = "/singleSendSms";
		String method = "GET";
		String appcode = "81013ffb9274478c9bcc9329d2db3bd7";
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Authorization", "APPCODE " + appcode);

		try {
			HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);

			if (response.getStatusLine().getStatusCode() != 200) {
				System.out.println(
						"request url failed, http code=" + response.getStatusLine().getStatusCode() + ", url=" + host);
				return null;
			}
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String resultStr = EntityUtils.toString(entity, "utf-8");

				return JSONObject.parseObject(resultStr, MessageResultVO.class);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
}
